/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.PatVisitIndex;
import com.jims.master.modules.doctor.service.PatVisitIndexService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatVisitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 病案首页基本信息Controller
 * @author 赵宁
 * @version 2017-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/patVisitIndex")
public class PatVisitIndexController extends BaseController {

	@Autowired
	private PatVisitIndexService patVisitIndexService;
	@Autowired
	private PatVisitService patVisitService;
	
	@ModelAttribute
	public PatVisitIndex get(@RequestParam(required=false) String id) {
		PatVisitIndex entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = patVisitIndexService.getIndex(id);
		}
		if (entity == null){
			entity = new PatVisitIndex();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PatVisitIndex patVisitIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PatVisitIndex> page = patVisitIndexService.findPage(new Page<PatVisitIndex>(request, response), patVisitIndex); 
		model.addAttribute("page", page);
		return "modules/doctor/patVisitIndexIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(PatVisitIndex patVisitIndex, Model model) {
		model.addAttribute("patVisitIndex", patVisitIndex);
		return "modules/doctor/patVisitIndexForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PatVisitIndex patVisitIndex, Model model) {
	    StringData data=new StringData();
		int i=patVisitIndexService.save(patVisitIndex);
        if(i>0){
            data.setCode("success");
            data.setData("保存病案首页基本信息成功");
        }else{
            data.setCode("error");
            data.setData("保存病案首页基本信息失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PatVisitIndex patVisitIndex) {
	    StringData data=new StringData();
		int i=patVisitIndexService.delete(patVisitIndex);
        if(i>0){
            data.setCode("success");
            data.setData("删除病案首页基本信息成功");
        }else{
            data.setCode("error");
            data.setData("删除病案首页基本信息失败");
        }
		return data;
	}

	/**
	 * 入院患者登记-修改病人基本信息
	 * @param patVisitIndex
	 * @param model
	 * @return
	 * @author CTQ
	 * @date 2017-07-19 15:58:48
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "updatePatientInfo")
	public String updatePatientInfo(PatVisitIndex patVisitIndex, Model model) {
		model.addAttribute("patVisitIndex", patVisitIndex);
		return "modules/doctor/hospitalPatientInfoForm";
	}


	/**
	 * 入院患者登记-保存修改后的病人基本信息
	 * @param patVisitIndex
	 * @param model
	 * @return
	 * @author CTQ
	 * @date 2017-07-19 15:58:48
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "savePatient")
	@ResponseBody
	public StringData savePatient(PatVisitIndex patVisitIndex, Model model) {
		StringData data = new StringData();
		int i = patVisitIndexService.savePatient(patVisitIndex);
		if (i > 0) {
			data.setCode("success");
			data.setData("修改病人基本信息成功");
		} else {
			data.setCode("error");
			data.setData("修改病人基本信息失败");
		}
		return data;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "updateInfo")
	@ResponseBody
	public StringData updateInfo(PatVisitIndex patVisitIndex){
		int i = patVisitIndexService.updateInfo(patVisitIndex);
		StringData stringData = new StringData();
		if(i>0){
			stringData.setCode("success");
			stringData.setData("修改成功");
		}else{
			stringData.setCode("error");
			stringData.setData("修改失败");
		}
		return stringData;
	}

}